package model;

import java.net.InetAddress;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class FileServerData implements Comparable<FileServerData>{
	
	private InetAddress address;
	private int port;
	private long usage;
	private boolean online;
	private long lastAlivePackage;
	private Set<String> fileNames;

	public FileServerData(InetAddress address, int port) {
		this(address, port, new HashSet<String>());
	}
	
	public FileServerData(InetAddress address, int port, Set<String> fileNames) {
		this.address = address;
		this.port = port;
		this.fileNames = fileNames;
		usage = 0;
		online = true;
		lastAlivePackage = new Date().getTime();
	}

	public InetAddress getAddress() {
		return address;
	}

	public int getPort() {
		return port;
	}

	public long getUsage() {
		return usage;
	}
	
	public Set<String> getFileNames() {
		return fileNames;
	}
	
	public synchronized void addFileName(String fileName) {
		fileNames.add(fileName);
	}
	
	public synchronized void setFileNames(Set<String> fileNames) {
		this.fileNames = new HashSet<String>(fileNames);
	}

	public synchronized void addUsage(long usage) {
		this.usage += usage;
	}

	public boolean isOnline() {
		return online;
	}

	public synchronized void setOnline(boolean online) {
		this.online = online;
	}

	/**
	 * @return Timestamp of last Alive Package recieved
	 */
	public long getLastAlivePackage() {
		return lastAlivePackage;
	}

	/**
	 * @param lastAlivePackage Timestamp of last Alive Package recieved
	 */
	public synchronized void setLastAlivePackage(long lastAlivePackage) {
		this.lastAlivePackage = lastAlivePackage;
	}
	
	public FileServerInfo getFileServerInfo() {
		return new FileServerInfo(address, port, usage, online);
	}

	@Override
	public int compareTo(FileServerData other) {
		return (this.usage > other.usage ? 1 : (this.usage == other.usage ? 0 : -1));
	}
}
